﻿#define _CRT_SECURE_NO_WARNINGS 1

//Q4
// 
//假设环境是x64环境，程序输出的结果是啥？
//
//指针-指针 
#include<stdio.h>
int main()
{
	int a[5][5];
	int(*p)[4];//p是一个数组指针，p指向的数组是4个整型元素的

	p = a;//类型的差异	警告
	//int(*)[5]		int(*)[4]

	printf("%d,%p\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);

	return 0;
}


//整型指针-1向前移动一个整型


//Q5
#include <stdio.h>
int main()
{
	int aa[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int* ptr1 = (int*)(&aa + 1);
	int* ptr2 = (int*)(*(aa + 1));
	printf("%d,%d", *(ptr1 - 1), *(ptr2 - 1));//10 5

	return 0;
}

//Q6
#include <stdio.h>
int main()
{
	char* a[] = { "work","at","alibaba" };
	char** pa = a;
	pa++;
	printf("%s\n", *pa);

	return 0;
}


//%s从给出的地址开始打印直到\0
//
//Q7
//
#include <stdio.h>
int main()
{
	char* c[] = { "ENTER","NEW","POINT","FIRST" };
	char** cp[] = { c + 3,c + 2,c + 1,c };
	char*** cpp = cp;

	printf("%s\n", **++cpp);//POINT
	printf("%s\n", *-- * ++cpp + 3);//ER
	printf("%s\n", *cpp[-2] + 3);//ST
	printf("%s\n", cpp[-1][-1] + 1);//EW

	return 0;
}



#include<ctype.h>

int main()
{
	//int ret = islower('A');
	int ret = isspace('	');

	printf("%d\n", ret);

	return 0;
}

//写一个代码，将字符串中的小写字⺟转大写，其他字符不变。

int main()
{
	char arr[] = "I am a student.";
	int i = 0;
	while (arr[i] != '\0')
	{
		if (islower(arr[i]));
		{

			arr[i] -= 32;
		}
		//arr[i] = toupper(arr[i]);
		i++;
	}

	return 0;
}

//strlen求的是字符串的长度，统计的是字符串中\0之前的字符个数,字符串中必须有\0
//size_t strlen(const char* str)

int main()
{
	//	  size_t			  size_t
	//		3		  -			6
	if (strlen("abc") - strlen("abcdef") > 0)
	//if ((int)strlen("abc") - (int)strlen("abcdef") > 0)
	{
		printf("str1>str2\n");
	}
	else
	{
		printf("str1<=str2\n");
	}

	return 0;
}


//strlen模拟实现
// 
//1.计数器的方式
#include<assert.h>

size_t my_strlen1(const char* str)
{
	int count = 0;
	assert(str != NULL);
	while (*str != '\0')
	{
		str++;
		count++;
	}
	return count;
}

//2.指针-指针
size_t my_strlen2(const char* str)
{
	assert(str);
	char* start = str;
	while (*start != '\0')
	{
		start++;
	}
	return start - str;
}

//3.不创建临时变量 - 递归的方式
int my_strlen3(const char* str)
{
	assert(str);
	if (*str == '\0')
		return 0;
	else
		return 1 + my_strlen(str + 1);
}

int main()
{
	printf("%zd\n", my_strlen1("abedef"));
	printf("%zd\n", my_strlen2("abedef"));
	printf("%zd\n", my_strlen3("abedef"));

	return 0;
}



//char* strcpy(char* destination, const char* source);

int main()
{
	char arr1[20] = "Wow, ";
	char arr2[] = "fantastic!";

	strcpy(arr1, arr2);

	printf("%s\n", arr2);

	return 0;
}

//模拟实现strcpy
#include<string.h>

char* my_strcpy(char* dest, const char* src)
{
	assert(src != NULL);
	assert(dest != NULL);
	char* ret = dest;
	//while (*src!='\0')
	//{
	//	*dest++ = *src++;
	//}
	// *dest = *src;
	while (*dest++ = *src++)//'\0'ASCII码值为0
	{
		;
	}
	return ret;
}
int main()
{
	char arr1[20] = "Wow, ";
	char arr2[] = "fantastic!";

	char* ret = my_strcpy(arr1, arr2);

	printf("%s\n", arr2);
	printf("%s\n", ret);

	return 0;
}



//strcat函数用来连接字符串
// 返回的是目标空间的起始地址
//
//char* strcat(char* destination, const char* source)；
//
int main()
{
	char arr1[20] = "Wow, ";//Wow, fantastic!
	char arr2[] = "fantastic!";
	strcat(arr1, arr2);
	printf("%s\n", arr1);

	return 0;
}

//模拟实现strcat
my_strcat(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;
	while (*dest != '\0')
		dest++;
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}
int main()
{
	char arr1[20] = "Wow, ";//Wow, fantastic!
	char arr2[] = "fantastic!";
	my_strcat(arr1, arr2);
	printf("%s\n", arr1);

	return 0;
}